package com.apes.fn.scm.retailOrder.service.tc.tcOutbox;


import com.alibaba.fastjson.JSONObject;
import com.apes.fn.scm.retailOrder.service.tc.TCDao;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.DateUtil;
import com.apes.framework.util.MapUtil;
import com.apes.hr.base.model.Leave;
import com.apes.hr.base.model.Rank;
import com.apes.hr.transfer.model.QuitJobOrder;
import com.apes.scm.rbac.model.User;
import com.apestech.framework.db.SqlSession;
import com.apestech.framework.util.DBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 功能：平台外接送车提成
 *
 * @author linwz
 * @create 2021-03-15 16:56
 */
@Service("tcOutPickCarService")
public class TcOutPickCarService extends DomainService {
    @Autowired
    TcOutPickCarRepository tcOutPickCarRepository;
    @Autowired
    TCDao tcDao;

    public TcOutPickCar save(SimpleRequest request) {
        TcOutPickCar rank = request.getO(TcOutPickCar.class);
        rank.setState("create");
        return tcOutPickCarRepository.saveAndFlush(rank);
    }

    public TcOutPickCar update(SimpleRequest request) {
        String id = request.get("id");
        int version = request.get("version");
        TcOutPickCar rank = tcOutPickCarRepository.findOne(id);
        if (rank == null)  throw new RuntimeException("记录【" + id + "】在系统中不存在。");
        if (rank.getVersion() != version) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        TcOutPickCar rankDate = request.getO(TcOutPickCar.class);
        return tcOutPickCarRepository.saveAndFlush(rankDate);
    }

    public TcOutPickCar delete(SimpleRequest request) {
        TcOutPickCar rank = request.getO(TcOutPickCar.class);
        rank.setState("delete");
        return tcOutPickCarRepository.saveAndFlush(rank);
    }

    public Page<TcOutPickCar> findAll(SimpleRequest request) {
        List<Map> filters = request.get("condition");
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return tcOutPickCarRepository.findAll(JpaDsl.toCriteria(filters), PageRequest.of(request.get("page"), request.get("size"), sort));
    }
    public Page<TcOutPickCar> find(SimpleRequest request) {
        return tcOutPickCarRepository.findAll(PageRequest.of(request.get("page"), 100) );
    }
    public TcOutPickCar findById(String id) {
        return tcOutPickCarRepository.findOne(id);
    }

    public TcOutPickCar findOne(SimpleRequest request) {
        return tcOutPickCarRepository.findOne((String) request.get("id"));
    }

    /***
     * 功能 转OA start 节点
     * @param request
     * @return
     * @throws RuntimeException
     */
    public TcOutPickCar transOA(SimpleRequest request) throws RuntimeException{
        //设置单据状态为处理中
        TcOutPickCar tcOutPickCar = request.getO(TcOutPickCar.class);  //设置javaBean 并格式化时间
        // 设置转OA人
        tcOutPickCar.setState("disposing");  //设置当前的状态为处理中

        tcOutPickCar = tcOutPickCarRepository.saveAndFlush(tcOutPickCar);
        return tcOutPickCar;
    }

    public TcOutPickCar approve(SimpleRequest request){
        String s = request.get("wf_variable_wf_state");
        TcOutPickCar tcOutPickCar = request.getO(TcOutPickCar.class);
        Set<TcOutPickCarItem> tcOutPickCarItems = tcOutPickCar.getTcOutPickCarItems();
        Date date = new Date();
        if (s.equals("1")){
            // 生成接送车提成
//            tcOutPickCarRepository;\
            tcOutPickCarItems.forEach(tcOutPickCarItem -> {
                JSONObject param =  new JSONObject();
                param.put("TJRQ",date);
                param.put("JCRQ",date);
                param.put("FN_INNER_PICK_DEL_UP_CAR_ID",tcOutPickCar.getId());
                param.put("PLATE_LICENSE",tcOutPickCarItem.getPlateNumber());
                param.put("COMPANY_ID",tcOutPickCar.getDept().getCompany().getId());
                param.put("DEPT_ID_MD",tcOutPickCar.getDept());
                param.put("DEPT_ID_JD",tcOutPickCarItem.getCooperation());
                param.put("JSCR",tcOutPickCar.getVisitor().getId());
                param.put("BZ","接车");
                param.put("TC_AMT",tcOutPickCarItem.getPickUpMoney());
//                param.put("DISPATCHER_DEPT_ID",null);
                tcDao.inserTcOutPickCar(param);
                param.put("BZ","归还");
                param.put("TC_AMT",tcOutPickCarItem.getPickOutMoney());
                tcDao.inserTcOutPickCar(param);
            });
            tcOutPickCar.setState("approve");
            tcOutPickCar.setApproveDate(new Date());
            tcOutPickCar.setApproveUid(request.getPerson());
            return tcOutPickCarRepository.saveAndFlush(tcOutPickCar);

        } else {
            tcOutPickCar.setState("cancel");
            return tcOutPickCarRepository.saveAndFlush(tcOutPickCar);
        }
    }


}
